package com.material.vtool.controller;

import com.material.vtool.entity.reqEntity.SelectedMaterialBean;
import com.material.vtool.entity.reqEntity.TeamMaterialShareLogReqBean;
import com.material.vtool.entity.respEntity.RespData;
import com.material.vtool.entity.respEntity.ReturnCode;
import com.material.vtool.service.UserShareMaterialLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 文章分享
 */
@RestController
@Slf4j
@RequestMapping("/log")
public class UserShareMaterialLogController {
    @Autowired
    private UserShareMaterialLogService userShareMaterialLogService;

    /**
     * 首页文章分享
     * @param userId 用户ID
     * @param selectedMaterialBean 分享文章实体
     * @return
     */
    @PostMapping(value = "/materialShare", produces = "application/json;charset=UTF-8")
    public RespData materialShare(@RequestHeader("userId") String userId, @RequestBody SelectedMaterialBean selectedMaterialBean) {
        if (selectedMaterialBean == null || selectedMaterialBean.getMaterialId() == null) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数错误");
        }

        userShareMaterialLogService.addUserMaterialShare(Integer.valueOf(userId), selectedMaterialBean.getMaterialId());

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 团队素材分享
     * @param userId 分享人ID
     * @param teamMaterialShareLogReqBean 请求实体
     * @return
     */
    @PostMapping(value = "/teamMaterialShare", produces = "application/json;charset=UTF-8")
    public RespData teamMaterialShare(@RequestHeader("userId") Integer userId, @RequestBody TeamMaterialShareLogReqBean teamMaterialShareLogReqBean) {
        if (teamMaterialShareLogReqBean == null || teamMaterialShareLogReqBean.getMaterialId() == null || teamMaterialShareLogReqBean.getMaterialId().intValue() <= 0
            || teamMaterialShareLogReqBean.getTeamId() == null || teamMaterialShareLogReqBean.getTeamId().intValue() <= 0) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        userShareMaterialLogService.addUserTeamMaterialShare(userId, teamMaterialShareLogReqBean.getMaterialId(), teamMaterialShareLogReqBean.getTeamId());

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }
}
